package com.filter;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Calendar;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.weixin4j.Configuration;

import beans.sys.UserBean;
import beans.user.user;
import beans.user.xhlog;
import service.SpringContextUtil;
import service.user.XHLogServiceImpl;

public class LoginFilter implements Filter{
	public void destroy() {

	}

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)res;

		String requesturi = request.getRequestURI();
		if(isStaticSource(requesturi)){
			chain.doFilter(req, res);
			return;
		}
		if(requesturi.indexOf("/wxcall/")>-1
				||requesturi.endsWith("wxsubject/free")
				||requesturi.endsWith("wxpay/result")
				||requesturi.endsWith("wxpay/payindex")
				||requesturi.endsWith("wx/share")
				||requesturi.endsWith("wx/bindxh")
				||requesturi.indexOf("/tg/")>-1
				||requesturi.indexOf("/test/")>-1
				||requesturi.endsWith("wxsubject/errorsub")
				||requesturi.endsWith("wxsubject/subjectinfo")) {
			chain.doFilter(req, res);
			return;
		}
		if(requesturi.indexOf("/wx/")>-1||requesturi.indexOf("/wxsubject/")>-1||requesturi.indexOf("/wxpay/")>-1){//微信
			user user = (user) request.getSession().getAttribute("wxuser");
			if(user==null
					&&!requesturi.endsWith("wx/index")
					) {
				//response.sendRedirect(request.getContextPath()+"/wx/index");
				response.sendRedirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+Configuration.getOAuthAppId()+"&redirect_uri=http%3a%2f%2f2.zydkj.net.cn%2fwx%2findex&response_type=code&scope=snsapi_base&state=123#wechat_redirect");
				return;
			}
			//判断是否是考试
			if(requesturi.endsWith("wxsubject/drivertype")||requesturi.endsWith("wxsubject/kaoshi")) {//判断是否是题目的页面，如果是则判断是否已经付款等
				XHLogServiceImpl<xhlog> xhLogServiceImpl = (XHLogServiceImpl<xhlog>)SpringContextUtil.getApplicationContext().getBean("XHLogServiceImpl");
				xhlog _xhlog = xhLogServiceImpl.getByUid(user.getOpenid());
				Calendar cal = Calendar.getInstance();
				cal.add(Calendar.HOUR, -2);
				if(_xhlog==null||!cal.getTime().before(_xhlog.getAddtime())) {
					response.sendRedirect(request.getContextPath()+"/wx/xh?url="+URLEncoder.encode(requesturi+"?"+request.getQueryString(),"UTF-8"));
					return;
				}
			}
			
			chain.doFilter(req, res);
			return;
		}
		UserBean user = (UserBean) request.getSession().getAttribute("user");
		if(user==null
				&& !requesturi.endsWith("/login.jsp")
				&& !requesturi.endsWith("/login/login")
				&& !requesturi.endsWith("/login/VerifyCode")
				&& !requesturi.endsWith(request.getContextPath()+"/")){
			response.sendRedirect(request.getContextPath()+"/login.jsp");
			return;
		}else{
			chain.doFilter(req, res);
		}
	}

	private boolean isStaticSource(String url){
		url = url.toLowerCase();
		return url.lastIndexOf(".js")>-1||url.lastIndexOf(".css")>-1||url.lastIndexOf(".jpg")>-1||url.lastIndexOf(".mp3")>-1
				||url.lastIndexOf(".gif")>-1||url.lastIndexOf(".png")>-1||url.lastIndexOf(".jpeg")>-1||url.lastIndexOf(".txt")>-1
				||url.lastIndexOf(".ico")>-1||url.lastIndexOf(".swf")>-1||url.lastIndexOf(".htm")>-1
				||url.lastIndexOf(".html")>-1||url.lastIndexOf(".woff")>-1||url.lastIndexOf(".ttf")>-1;
	}

	public void init(FilterConfig arg0) throws ServletException {
	}
}
